#include "modeldev.h"
#include "ui_modeldev.h"
#include "node.h"
#include "edge.h"
#define MD_TEXT_MAX_LENGTH 128
#define MD_DESC_MAX_LENGTH 512
#define MD_NAME_MAX_LENGTH 128
ModelDev::ModelDev(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ModelDev)
{
    ui->setupUi(this);
	ui->pTE_ModelDesc->Maxlength = MD_DESC_MAX_LENGTH;
	ui->pTE_ModelName->Maxlength = MD_NAME_MAX_LENGTH;
	ui->pTE_ModelText->Maxlength = MD_TEXT_MAX_LENGTH;
	ui->pTE_ModelDesc->bOneline = false;
}

ModelDev::~ModelDev()
{
    delete ui;
}

void ModelDev::OnAction_New()
{
	QList<QGraphicsItem*> items=ui->graphicsView->scene()->selectedItems();
	Node* node = new Node(ui->graphicsView);
	ui->graphicsView->scene()->addItem(node);
	if (items.length()==1)
	{
		ui->graphicsView->scene()->addItem(new Edge(node,qgraphicsitem_cast<Node*>( items.first())));
		
	}
	node->setPos(100,100);
}

void ModelDev::OnAction_Line2p()
{
	QList<QGraphicsItem*> items=ui->graphicsView->scene()->selectedItems();
	if (items.length()==2)
	{
		foreach (QGraphicsItem *item, ui->graphicsView->scene()->items()) 
		{
			if (Edge *edge = qgraphicsitem_cast<Edge *>(item))
			{
				if ( (edge->sourceNode() == qgraphicsitem_cast<Node*>( items.first())
					&&edge->destNode() == qgraphicsitem_cast<Node*>( items.last())) 
					||(	edge->sourceNode() == qgraphicsitem_cast<Node*>( items.last())
					&& edge->destNode() == qgraphicsitem_cast<Node*>( items.first()))
					)
				{
					return;
				}
			}
		}
		ui->graphicsView->scene()->addItem(new Edge(qgraphicsitem_cast<Node*>( items.first()),qgraphicsitem_cast<Node*>( items.last())));
	}
}

void ModelDev::OnAction_Delete()
{
	QList<QGraphicsItem*> items=ui->graphicsView->scene()->selectedItems();
	if (items.empty())
	{
		return;
	}

}


